package com.weixingtu.commuterCar.servlet;

import com.weixingtu.commuterCar.bean.Message;
import com.weixingtu.commuterCar.bean.Site;
import com.weixingtu.commuterCar.service.SiteService;
import com.weixingtu.commuterCar.service.impl.SiteServiceImpl;
import com.weixingtu.commuterCar.util.JSONUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/getSite.do")
public class GetSiteServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SiteService service = new SiteServiceImpl();
        List<Site> list = service.findAll();
        Message msg = new Message();
        if(list == null || list.size() == 0){
            msg.setStatus(-1);
            msg.setResult("查询结果为空");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list);
        }
        String json = JSONUtil.toJSON(msg);
        PrintWriter writer = response.getWriter();
        writer.print(json);
    }
}
